`
https://leetcode.cn/problems/get-equal-substrings-within-budget/
`

/**
 * @param {string} s
 * @param {string} t
 * @param {number} maxCost
 * @return {number}
 */
var equalSubstring = function (s, t, maxCost) {
  const n = s.length
  let res = 0
  let cost = 0
  const calcCost = (c1, c2) => Math.abs(c1.charCodeAt(0) - c2.charCodeAt(0))
  let left = 0, right = 0

  while (right < n) {
    cost += calcCost(s[right], t[right])
    right++

    // 缩小窗口
    while (cost > maxCost) {
      cost -= calcCost(s[left], t[left])
      left++
    }

    // 此时 cost <= maxCost ，更新答案
    res = Math.max(res, right - left)
  }

  return res
};